
/////////////////////////////////////////////////////////////////
//File: Login.cs
//Description: 
//  
//Copyright© 2011 Moliu
//History:
//  2012-1-3 14:20:54    CodeSmith5.0.1    Original Version
/////////////////////////////////////////////////////////////////


using System;
using System.Threading;
using System.Collections.Generic;
using System.IO;
using MAF.Common;
using My.Blog.Domain.Entities;
using My.Blog.Domain.Interfaces;
using My.Blog.Business.Service;
using My.Blog.Business.Security;
using My.Blog.Web.Controllers;
using System.Configuration;
using System.Collections.Specialized;
using System.Web;

namespace My.Blog.Web.UI
{
    public class Login : My.Blog.Web.Controllers.BasePage
    {
        public Login()
        {
            HttpContext context = HttpContext.Current;

            // 标签
            const string IF_LOGON = "If:Logon";
            const string IF_NOT_LOGON = "IfNot:Logon";

            // 模板路径及逻辑标签正则表达式
            string TPL_FILE_PATH = ConfigurationManager.AppSettings["TemplatesPath"] + @"/Login.html";
            const string REG_LOGIC = @"(?is)<(Tag>)(.*?)</\1";

            // 主模板
            string tpl = File.ReadAllText(context.Server.MapPath(TPL_FILE_PATH));
            Template tplLogic = new Template(tpl, REG_LOGIC);
            Template tplEntity;

            // 模板替换
            if (true == SecurityUtility.IsLogon)
            {
                tplLogic.Replace(IF_NOT_LOGON, "");
            }
            else
            {
                tplLogic.Replace(IF_LOGON, "");
            }

            // 登录或退出后的去向，通过ajax来实现的。
            string toUrl = "ArticleList.aspx";
            Uri uri = context.Request.UrlReferrer;
            if (null != uri)
            {
                string path = uri.GetLeftPart(UriPartial.Path).ToLower();
                if (!path.EndsWith("Login.aspx".ToLower())
                    && !path.EndsWith("/")
                    && !path.EndsWith("Register.aspx".ToLower())
                   )
                { toUrl = uri.ToString(); }
            }

            tplEntity = new Template(tplLogic.Result);
            NameValueCollection infoMap = new NameValueCollection();

            infoMap["{RootUrl}"] = this.RootUrl;
            infoMap["{ToUrl}"] = toUrl;
            tplEntity.Replace(infoMap);

            context.Response.Write(tplEntity.Result);
        }
    }
}